package com.whut.work.user.service.impl;

import com.whut.work.base.model.Page;
import com.whut.work.enlist.model.Enlist;
import com.whut.work.exam.model.Exam;
import com.whut.work.user.dao.impl.UserDaoImpl;
import com.whut.work.user.model.User;
import com.whut.work.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zouy on 17-3-15.
 */
@Component
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserDaoImpl userDao;

    public Page<User> getUserPageList(int currentPage, int pageSize, String blurUserName) throws Exception {
        String queryHql = "from User u where u.username like '%"+blurUserName+"%' ";
        String countHql = "select count(*) from User u where u.username like '%"+blurUserName+"%' ";
        Page<User> returnPage = userDao.findPage(currentPage, pageSize, queryHql, countHql);

        return returnPage;
    }

    public Map<String, Object> getCoursePageListByUserId(int currentPage, int pageSize, Integer userId) throws Exception {
        Map<String,Object> returnMap = new HashMap<String,Object>();
        String hql = "from User u where u.id='"+userId+"' ";
        User user = userDao.findOne(hql);
        if(user != null){
            returnMap.put("value", user);
            returnMap.put("message", "获取成功");
            returnMap.put("success", true);
        }else {
            returnMap.put("message", "错误");
            returnMap.put("success", false);
        }

        return returnMap;
    }

    public Map<String, Object> addOneUser(String password, String type, String username, String sex,
                                          String origin, String graduate, String major, String tel, String picture,
                                            String number) throws Exception {
        Map<String,Object> returnMap = new HashMap<String,Object>();
        User user = new User();
        Enlist enlist = new Enlist();
        Exam exam = new Exam();

        enlist.setType(type);
        enlist.setUsername(username);
        enlist.setSex(sex);
        enlist.setOrigin(origin);
        enlist.setGraduate(graduate);
        enlist.setMajor(major);
        enlist.setTel(tel);
        enlist.setPicture(picture);
        user.setEnlist(enlist);

        exam.setNumber(number);
        exam.setExamTime(new Date());
        exam.setExamPlace("武汉市八分山考场");
        exam.setResult(80);
        user.setExam(exam);

        user.setAccount(number);
        user.setPassword(password);
        user.setCreateTime(new Date());
        user.setAfterLogin("new");
        userDao.save(user);

        returnMap.put("value", user);
        returnMap.put("message", "注册成功");
        returnMap.put("success", true);

        return returnMap;
    }

    public Map<String, Object> updateOneUser(String account, String password, String type, String username, String sex, String origin,
                                             String graduate, String major, String tel, String picture) throws Exception {
        Map<String,Object> returnMap = new HashMap<String,Object>();
        Exam exam = new Exam();

        User user = userDao.findOne(" from User u where u.account='"+account+"' ");
        if(user != null){
            user.setPassword(password);

            Enlist enlist = user.getEnlist();

            enlist.setType(type);
            enlist.setUsername(username);
            enlist.setSex(sex);
            enlist.setOrigin(origin);
            enlist.setGraduate(graduate);
            enlist.setMajor(major);
            enlist.setTel(tel);
            enlist.setPicture(picture);
            user.setEnlist(enlist);

            userDao.update(user);
            returnMap.put("message", "更新成功");
            returnMap.put("success", true);
        }

        return returnMap;
    }

    public Map<String, Object> getOneUser(String account, String password) throws Exception {
        Map<String,Object> returnMap = new HashMap<String,Object>();
        User user = userDao.findOne(" from User u where u.account='"+account+"' and u.password='"+password+"' ");
        if(user != null){
            returnMap.put("value", user);
            returnMap.put("message", "获取成功");
            returnMap.put("success", true);
        }else {
            returnMap.put("message", "失败");
            returnMap.put("success", false);
        }

        return returnMap;
    }


}
